<?php

/**
 * 访问控制类
 * @author Administrator
 *
 */
class Acl extends CI_Controller
{
    protected $CI;

    /**
     *
     */
    function __construct()
    {
        //parent::__construct ();
    }

    function sync_session()
    {
        if (isset($_REQUEST['SESSID'])) {
            //session_id($_REQUEST['SESSID']);
        }
        //var_dump($_REQUEST);
    }

    function check_privilege()
    {
        $this->CI  =& get_instance();
        $directory = $this->CI->router->directory;
        $class     = $this->CI->router->class;
        $method    = $this->CI->router->method;
// 		$admin_id = isset($_SESSION['admin_id']) ? intval($_SESSION['admin_id']) : 0;
// 		$admin_name = isset($_SESSION['admin_name']) ? trim($_SESSION['admin_name']) : '';

        $exclude_controller          = array();
        $exclude_controller[]        = 'privilege';
        $exclude_method['privilege'] = array('signin', 'logout', 'admin_authorize');

        $is_process = TRUE;
        if (in_array($class, $exclude_controller)) {
            $is_process = FALSE;
        }
        if (isset($exclude_method[$class]) && !in_array($method, $exclude_method[$class])) {
            $is_process = FALSE;
        }

        //防止重复登录，如果已登录状态再次进入登录页面则直接进入首页
        if ($method == 'signin' && !$this->check_session()) {
            redirect('./');
        }

        if ($is_process && $this->check_session()) {
            $go_login = TRUE;
            //判断是否自动登录
            $autoLogin = $this->CI->input->cookie('lcikp_si', TRUE);
            $autoLogin = 0; //关闭自动登录
            if ($autoLogin > 0) {
                $this->CI->load->model('privilege_model');
                $go_login = $this->CI->privilege_model->cookie_auto_login();
            }
            //if(empty($_SESSION['module_list'])){
            //	$go_login = TRUE;
            //}
            if ($go_login) {
                $this->access_deny();
            }
        }
        //同步session_id到记录表
        if (!empty($_SESSION['sid']) && $_SESSION['sid'] != session_id()) {
            $this->CI->load->model('privilege_model');
            $update_datas['last_session'] = session_id();
            $this->CI->privilege_model->edit_data($update_datas, $_SESSION['user_id']);
            $_SESSION['sid'] = $update_datas['last_session'];
        }
    }

    function access_deny($message = '')
    {
        $redirect_url = 'privilege/signin';
        $is_ajax      = intval($this->CI->input->is_ajax_request());
        if ($this->CI->router->class != $this->CI->router->default_controller && !$is_ajax) {
            $redirect_url .= '/?redirect=' . rawurlencode(site_url($this->CI->router->uri->uri_string));
        }
        $message = empty($message) ? '登录超时！请重新登录！' : $message;
        if ($is_ajax > 0) {
            make_json_error($message, array('redirect' => site_url($redirect_url)));
        } else {
            $links = array('text' => "返回登录", 'href' => $redirect_url);
            show_message($message, $links, 'error');
        }
    }

    function check_session()
    {
        //$admin_id = isset($_SESSION['admin_id']) ? intval($_SESSION['admin_id']) : 0;
        //$admin_name = isset($_SESSION['admin_name']) ? trim($_SESSION['admin_name']) : '';
//        return (empty($_SESSION['user_id']) || empty($_SESSION['mobile']) || empty($_SESSION['module_list']));
        return (empty($_SESSION['user_id']));
    }

    /**
     * 记录用户行为日志
     */
    public function usrr_all_log()
    {
        $uri = uri_string();
        if (empty($uri)) return;
        $log_data = [
            'action'      => $uri,
            'action_name' => '',
            'param'       => !empty($_POST) ? json_encode($_POST) : (!empty($_GET) ? json_encode($_GET) : ''),
            'user_id'     => isset($_SESSION['user_id']) ? $_SESSION['user_id'] : '',
            'ip'          => isset($_SESSION['ip']) ? $_SESSION['ip'] : '',
            'add_time'    => time()
        ];
        $this->CI->db->insert('action_log', $log_data);
    }

    /**
     * 记录用户行为日志，这里用的是自定义的日志记录
     */
    public function user_action_log()
    {
        // 获取当前操作的uri
        $uri = trim(uri_string(), '/');
//        echo $uri;die;
        // 加载配置文件
        $this->CI->config->load('action_log', TRUE);
        $action_log_name = $this->CI->config->item('action_log_name', 'action_log');
        $check_ajax_list = $this->CI->config->item('check_ajax_list', 'action_log');
        $check_post_list = $this->CI->config->item('check_post_list', 'action_log');
        // 如果操作的uri在$check_ajax_list这个数组中，就是我们要记录的日志，因为uri可能后面是带参数的，所以这里有一个或

        // 这里是插叙如有有两个参数，e_account/ajax/do_unbind_user/32/5482
        $temp   = substr($uri, 0, strrpos($uri, '/')); // 这个是只有一个参数
        $temp_2 = substr($temp, 0, strrpos($temp, '/')); // 这个是两个参数

        if (in_array($temp, $check_ajax_list) || in_array($temp_2, $check_ajax_list) || in_array($uri, $check_ajax_list)) {
            $log_data = [
                'action'      => $uri,
                'action_name' => isset($action_log_name[$temp]) ? $action_log_name[$temp] : '',
                'user_id'     => isset($_SESSION['user_id']) ? $_SESSION['user_id'] : '',
                'hospital_id' => isset($_SESSION['hospital_id']) ? $_SESSION['hospital_id'] : '',
                'ip'          => isset($_SESSION['ip']) ? $_SESSION['ip'] : '',
                'add_time'    => time()
            ];
            $this->CI->db->insert('mc_action_log', $log_data);
        } elseif (in_array($uri, $check_post_list)) {    //如果uri在post操作中，也是记录的日志
            $log_data = [
                'action'      => $uri . '/' . (isset($_POST['action']) ? $_POST['action'] : '') . '/' . (isset($_POST['id']) ? $_POST['id'] : '') . (isset($_POST['checkboxes']) ? '_' . implode('_', $_POST['checkboxes']) : ''),    //这里如果是批量操作的时候，我们要记录下批量操作的记录
                'action_name' => isset($action_log_name[$uri]) ? $action_log_name[$uri] : '',
                'user_id'     => isset($_SESSION['user_id']) ? $_SESSION['user_id'] : '',
                'hospital_id' => isset($_SESSION['hospital_id']) ? $_SESSION['hospital_id'] : '',
                'ip'          => isset($_SESSION['ip']) ? $_SESSION['ip'] : '',
                'add_time'    => time()
            ];
            $this->CI->db->insert('mc_action_log', $log_data);
        }
    }

}

?>